package com.jiandaoshou.http;

import android.app.Application;

import com.jiandaoshou.utils.JsonCallback;
import com.jiandaoshou.utils.LogUtil;
import com.jiandaoshou.utils.NetUtil;
import com.jiandaoshou.utils.ToastUtil;
import com.jiandaoshou.utils.ToolLoading;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

import static com.jiandaoshou.utils.NetUtil.checkNet;

/**
 * Created by Administrator on 2016/10/17.
 */

public class Http {

    private static Application applicationContext;

    public static final void init(Application context) {
        applicationContext = context;
    }

    private static String joinUrl(String dataType, HashMap<String, String> params) {
        return APIDefine.BASEURL + "?DataType=" + dataType + "&Data=" + createParams(params);
    }

    private static JSONObject createParams(HashMap<String, String> params) {
        try {
            JSONObject json = new JSONObject(params);
            return json;
        } catch (Exception e) {
        }
        return null;
    }

    public static final void  post(String dataType, HashMap<String, String> params, final HttpCallBack callback) {
        post(true, dataType, params, callback);
    }

    public static final void  postLoading(boolean showLoading,String dataType, HashMap<String, String> params, final HttpCallBack callback) {
        post(showLoading, dataType, params, callback);
    }


    public static final void  post(final boolean isShowloading, String dataType, HashMap<String, String> params, final HttpCallBack callback) {
         boolean isNetAva = NetUtil.checkNet(applicationContext);
         //如果网络不存在
         if(!isNetAva ){
             ToastUtil.toast("请检查网络连接.......");
             return ;
         }
        String url = joinUrl(dataType, params);
        LogUtil.e("post url:::" + url);
        if (isShowloading)
            ToolLoading.ShowLoading(applicationContext);
        OkGo.get(url).cacheMode(CacheMode.DEFAULT).execute(new JsonCallback() {
            @Override
            public void onSuccess(String str, Call call, Response response) {
                LogUtil.e(str);
                if (isShowloading)
                    ToolLoading.closeLoading(applicationContext);
                if (callback != null) {
                    try {
                        callback.onHttpSuccess(str);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e(e.toString());
                super.onError(call, response, e);
                if (isShowloading)
                    ToolLoading.closeLoading(applicationContext);
                    ToastUtil.toast(e.getMessage());
                if (callback != null)
                    callback.onHttpError(e.getMessage());
            }
        });
    }

    public static abstract class HttpCallBack {
        public abstract void onHttpSuccess(String response) throws Exception;

        public void onHttpError(String error) {

        }
    }

}
